﻿using System;
using System.IO;
using System.Xml.XPath;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DocX_WordDemo
{
    public static class FormFieldOp
    {
        public static void CreateFormField(string fileName, string outputFileName)
        {
            Document doc = new Document();
            doc.AddSection();
            Table table = doc.LastSection.AddTable(true);
            table.DefaultColumnsNumber = 2;
            table.DefaultRowHeight = 20;

            using (FileStream fs = File.OpenRead(fileName))
            {
                XPathDocument xDoc = new XPathDocument(fs);
                XPathNodeIterator nodes = xDoc.CreateNavigator().Select("/form/section");
                foreach (XPathNavigator node in nodes)
                {
                    TableRow row = table.AddRow();
                    row.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    Paragraph p = row.Cells[0].AddParagraph();
                    p.AppendText(node.GetAttribute("name", ""));

                    XPathNodeIterator fieldNodes = node.Select("field");
                    foreach (XPathNavigator fieldNode in fieldNodes)
                    {
                        TableRow fieldRow = table.AddRow();
                        fieldRow.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                        Paragraph label = fieldRow.Cells[0].AddParagraph();
                        label.AppendText(fieldNode.GetAttribute("label", ""));

                        fieldRow.Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                        Paragraph fieldPara = fieldRow.Cells[1].AddParagraph();
                        string fieldId = fieldNode.GetAttribute("id", "");
                        switch (fieldNode.GetAttribute("type", ""))
                        {
                            case "text":
                                TextFormField textform = fieldPara.AppendField(fieldId, FieldType.FieldFormTextInput) as TextFormField;
                                textform.DefaultText = "";
                                textform.Text = "";
                                break;
                            case "list":
                                DropDownFormField list = fieldPara.AppendField(fieldId, FieldType.FieldFormDropDown) as DropDownFormField;
                                XPathNodeIterator itemNodes = fieldNode.Select("item");
                                foreach (XPathNavigator itemNode in itemNodes)
                                {
                                    list.DropDownItems.Add(itemNode.SelectSingleNode("text()").Value);
                                }
                                break;
                            case "checkbox":
                                fieldPara.AppendField(fieldId, FieldType.FieldFormCheckBox);
                                break;
                        }

                    }
                    table.ApplyHorizontalMerge(row.GetRowIndex(), 0, 1);
                }
            }
            doc.SaveToFile(outputFileName);
        }

        public static void FillFormField(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            using (FileStream fs = File.OpenRead("../../Document/User.xml"))
            {
                XPathDocument xDoc = new XPathDocument(fs);
                XPathNavigator xNavi = xDoc.CreateNavigator().SelectSingleNode("/user");
                foreach (FormField field in doc.LastSection.Body.FormFields)
                {
                    string path = string.Format("{0}/text()", field.Name);
                    XPathNavigator node = xNavi.SelectSingleNode(path);
                    if (node != null)
                    {
                        switch (field.Type)
                        {
                            case FieldType.FieldFormTextInput:
                                field.Text = node.Value;
                                break;
                            case FieldType.FieldFormDropDown:
                                DropDownFormField combox = field as DropDownFormField;
                                for (int i = 0; i < combox.DropDownItems.Count; i++)
                                {
                                    if (combox.DropDownItems[i].Text == node.Value)
                                    {
                                        combox.DropDownSelectedIndex = i;
                                        break;
                                    }
                                    if (field.Name == "Country" && combox.DropDownItems[i].Text == "Others")
                                    {
                                        combox.DropDownSelectedIndex = i;
                                    }
                                }
                                break;
                            case FieldType.FieldFormCheckBox:
                                if (Convert.ToBoolean(node.Value))
                                {
                                    CheckBoxFormField check = field as CheckBoxFormField;
                                    check.Checked = true;
                                }
                                break;
                        }
                    }
                }
            }
            doc.SaveToFile(outputFileName);
        }

    }
}
